<?php
use yii\helpers\Html;
use yii\helpers\Url;
?>

  <div class="purchaseOrder">
    <div class="main">
      <div class="content-wrap">
        <h1 class="tt0">我的进货单 <img src="http://static.tianpinwang.com.cn/v1/images/shopping-icon.png" alt=""></h1>

        <?php if($carts):?>
        <div class="orderLists">
          <h1 class="tt">
            <span class="firstTt inlineBlock"><span class="selectAll check-box selected" onclick="select_all(this)"></span>全选</span>
            <span class="secondTt inlineBlock">货品</span>
            <span class="thirdTt inlineBlock">数量</span>
            <span class="fourTt inlineBlock">单价</span>
            <span class="fiveTt inlineBlock">小计</span>
            <span class="sixTt inlineBlock">操作</span>
          </h1>
          <div class="orders">
            <?php foreach( $carts as $v ):?>
            <div class="order">
              <span class="check-box selected" onclick="select_one(this)"></span>
              <div class="goodsInfo">
                <a href="/product/<?=$v['id']?>">
                  <span class="inlineBlock goodsName"><?=Html::encode($v['title'])?></span>
                  <img src="<?=$v['img_url']?>" width="80"  class="goodsImge">
                </a>
              </div>
              <div class="goodsNum" style="border:0;">
                <span class="selectNum"><?=Html::encode($v['product_num'])?></span>
<!--                <input type="number" value="1" class="number">-->
<!--                <span class="reduce inlineBlock disabled">-</span>-->
<!--                <span class="add inlineBlock">+</span>-->
              </div>
              <div class="goodsPrice">￥<span class="selectPrice"><?=number_format($v['product_price'] , 2, '.', '' );?></span>/<?=Html::encode($v['price_unit'])?></div>
              <div class="OneTotal">￥<span><?=number_format($v['product_price'], 2, '.', '' )*$v['product_num']?></span></div>
              <div class="goodsDelete"><i class="fa fa-trash-o orderDelete" data-id="<?=$v['id']?>" onclick="delete_one(this)"></i></div>
            </div>
            <?php endforeach;?>

          </div>


          <p class="settlement">
            <span class="firstTt inlineBlock"><span class="selectAll check-box selected" onclick="select_all(this)"></span>全选</span>
            <span class="deleteSelect" onclick="delete_all()">删除</span>
            <span class="right txt">共 <span class="totalCount"><?=$total['total_num']?></span>件商品，已选择 <span class="selectCount"><?=$total['total_num']?></span>件
              <span class="totalMoney">货品金额总计（不包含运费）：<em class="money">￥<?=$total['total_price']?></em></span></span>
            <a href="javascript:;" class="btn-middle-default setBtn" id="go_settle">去结算</a>
          </p>



          <div class="failItems">
            <?php /*
            <h1 class="tt0">失效的物品</h1>

            <div class="orders failItem">

              <div class="order">
                <div class="goodsInfo">
                  <a href="">
                    <span class="inlineBlock goodsName">多维保辣椒粉多维保辣椒粉多维保辣椒粉多维保辣椒粉</span>
                    <img src="http://static.tianpinwang.com.cn/v1/images/moment/orderImge.png" alt=""  class="goodsImge">
                  </a>
                </div>
                <div class="goodsNum ">
                  <input type="number" value="1" class="number">
                  <span class="reduce inlineBlock disabled">-</span>
                  <span class="add inlineBlock">+</span>
                </div>
                <div class="goodsPrice">￥23.00/袋</div>
                <div class="OneTotal">￥466.50</div>
                <div class="goodsDelete"><i class="fa fa-trash-o orderDelete"></i></div>
                <div class="failMask"></div>
              </div>

              <div class="order">
                <div class="goodsInfo">
                  <a href="">
                    <span class="inlineBlock goodsName">多维保辣椒粉多维保辣椒粉多维保辣椒粉多维保辣椒粉</span>
                    <img src="http://static.tianpinwang.com.cn/v1/images/moment/orderImge.png" alt=""  class="goodsImge">
                  </a>
                </div>
                <div class="goodsNum ">
                  <input type="number" value="1" class="number">
                  <span class="reduce inlineBlock disabled">-</span>
                  <span class="add inlineBlock">+</span>
                </div>
                <div class="goodsPrice">￥23.00/袋</div>
                <div class="OneTotal">￥466.50</div>
                <div class="goodsDelete"><i class="fa fa-trash-o orderDelete"></i></div>
                <div class="failMask"></div>
              </div>
            </div>
            */?>
          </div>


        </div>
        <?php else:?>
        <div class="noOrders">
          <p class="tt0">您的订货单还是空的，快去采购吧</p>
          <p class="btn"><a href="/" class="btn-middle-default">立即去采购</a></p>
          <img src="http://static.tianpinwang.com.cn/v1/images/iconfont-iconfontyonghu01.png" alt="">
        </div>
        <?php endif;?>
      </div>
    </div>

    <div class="mask"></div>

    <!--删除商品时-->
    <div class="deleteConfirm">
      <img src="http://static.tianpinwang.com.cn/v1/images/closeIcon1.png" alt="" class="close">
      <h1 class="tt">确定删除吗？</h1>
      <p class="btns"><a href="" class="btn-small-default">确定</a><a href="" class="btn-small-empty">取消</a></p>
    </div>


    <!--删除商品时，没有选择-->
    <div class="deleteConfirm noSelect">
      <img src="http://static.tianpinwang.com.cn/v1/images/closeIcon1.png" alt="" class="close">
      <h1 class="tt">请选择商品！</h1>
      <p class="btns"><a href="" class="btn-small-default">确定</a></p>
    </div>

  </div>

 

  
<script>
    //单选
    function select_one(_this){
        if(!$(_this).hasClass('selected')){
            $(_this).addClass('selected');
        }else {
            $(_this).removeClass('selected');
        }
        get_selected();
    }

    //全选
    function select_all(_this){
        if( !$(_this).hasClass('selected') ){
            $('.check-box').each(function(e,item){
                $(item).addClass('selected');
            });
        }else{
            $('.check-box').each(function(e,item){
                $(item).removeClass('selected');
            });
        }
        get_selected();
    }

    //删除一个
    function delete_one(_this){
        var id = $(_this).attr('data-id');
        $.ajax({
            type: "POST",
            url: "/shopping-cart/delete",
            data: "id="+id+"&_csrf=<?=Yii::$app->request->getCsrfToken()?>",
            dataType: "json",
            success: function(data){
                if(!data.errNo){
                    layer.msg(data.msg,{time:2000});
                    window.location.reload();
                }else{
                    layer.msg(data.msg,{time:2000});
                }
            }
        });
    }

    //删除选中的
    function delete_all(){
        var ids = '';
        $('.order .selected').each(function(e, item){
            ids += '-'+$(item).siblings('.goodsDelete').find('.orderDelete').attr('data-id');
        });
        $.ajax({
            type: "POST",
            url: "/shopping-cart/delete",
            data: "id="+ids+"&_csrf=<?=Yii::$app->request->getCsrfToken()?>",
            dataType: "json",
            success: function(data){
                if(!data.errNo){
                    layer.msg(data.msg,{time:2000});
                    window.location.reload();
                }else{
                    layer.msg(data.msg,{time:2000});
                }
            }
        });
    }

    //获取已选商品件数和已选的总价
    function get_selected(){
        var selected_num = 0;
        var selected_price = 0;
        $('.order .selected').each(function(e, item){
            selected_num += parseInt( $(item).siblings('.goodsNum').find('.selectNum').text() );
            selected_price += (parseFloat( $(item).siblings('.goodsPrice').find('.selectPrice').text() ) * parseInt( $(item).siblings('.goodsNum').find('.selectNum').text() ));
        });
        $('.selectCount').html(selected_num);
        $('.totalMoney .money').html('¥'+selected_price);

        if( !selected_num ){
            $('.settlement .setBtn').addClass('disabled');
            $('.settlement .setBtn').attr('href', 'javascript:;');
        }else{
            $('.settlement .setBtn').removeClass('disabled');
            $('.settlement .setBtn').attr('href', 'javascript:;');
        }
    }

    //去结算
    $('#go_settle').click(function(){
        var ids = '';
        $('.order .selected').each(function(e, item){
            ids += '-'+$(item).siblings('.goodsDelete').find('.orderDelete').attr('data-id');
        });
        window.location.href = "<?=Yii::$app->request->hostInfo?>/order/confirm?ids="+ids;
    });

</script>

